//
//  ListCell.swift
//  CodingTest
//
//  Created by LYPC on 2021/11/18.
//

import UIKit

extension ListCell: CTTableViewCellInitProtocol { }

class ListCell: UITableViewCell {
    
    private var cellModel: DataModel?
    
    private let contentL = UILabel() /// 与link公用
    private let imgContentView = UIView()
    private let imageV0 = UIImageView()
    private let imageV1 = UIImageView()
    private let imageV2 = UIImageView()
    private let separateLine = UIView()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupLayout()
        setupStyle()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - Reactive
extension ListCell: ViewModelConfigurable {
    
    func configure(_ model: DataModel) {
        self.cellModel = model
                
        contentL.isHidden = model.content.isEmpty
        contentL.text = model.content
        if model.type == .text_link && !model.content.isEmpty {
            let attrStr = model.content.attributedStr(font: .regular(15), color: .color(.link))
            attrStr.addUnderLine()
            contentL.attributedText = attrStr
        }
        
        let placeholder = UIImage(named: "placeholder_empty")
        
        imgContentView.isHidden = true
        imageV0.isHidden = true
        imageV1.isHidden = true
        imageV2.isHidden = true
        let imgCount = model.imgUrls.count
        if imgCount == 1 {
            imgContentView.isHidden = false
            imageV0.isHidden = false
            imageV0.kf.setImage(with: model.imgUrls[0].asUrl, placeholder: placeholder)
        } else if imgCount == 2 {
            imgContentView.isHidden = false
            imageV0.isHidden = false
            imageV1.isHidden = false
            imageV0.kf.setImage(with: model.imgUrls[0].asUrl, placeholder: placeholder)
            imageV1.kf.setImage(with: model.imgUrls[1].asUrl, placeholder: placeholder)
        } else if imgCount >= 3 {
            imgContentView.isHidden = false
            imageV0.isHidden = false
            imageV1.isHidden = false
            imageV2.isHidden = false
            imageV0.kf.setImage(with: model.imgUrls[0].asUrl, placeholder: placeholder)
            imageV1.kf.setImage(with: model.imgUrls[1].asUrl, placeholder: placeholder)
            imageV2.kf.setImage(with: model.imgUrls[2].asUrl, placeholder: placeholder)
        }
        updateLayout(model)
    }
}

// MARK: - UI Setup
private extension ListCell {
    func setupLayout() {
        clipsToBounds = true
        contentView.addSubview(contentL)
        contentView.addSubview(imgContentView)
        imgContentView.addSubview(imageV0)
        imgContentView.addSubview(imageV1)
        imgContentView.addSubview(imageV2)
        contentView.addSubview(separateLine)
        contentL.snp.makeConstraints { (m) in
            m.leading.trailing.top.equalToSuperview().inset(20.fitR)
            m.height.equalTo(0)
        }
        imgContentView.snp.makeConstraints { (m) in
            m.leading.trailing.equalToSuperview().inset(20.fitR)
            m.top.equalTo(contentL.snp.bottom).offset(15.fitR)
            m.height.equalTo(80.fitR)
        }
        imageV0.snp.makeConstraints { (m) in
            m.edges.equalToSuperview()
        }
        imageV1.snp.makeConstraints { (m) in
            m.edges.equalToSuperview()
        }
        imageV2.snp.makeConstraints { (m) in
            m.edges.equalToSuperview()
        }
        separateLine.snp.makeConstraints { (m) in
            m.leading.trailing.equalToSuperview().inset(16.fitR)
            m.bottom.equalToSuperview()
            m.height.equalTo(1)
        }
    }
    
    func updateLayout(_ model: DataModel) {
        if model.type == .img {
            imgContentView.snp.remakeConstraints { (m) in
                m.edges.equalToSuperview().inset(20.fitR)
            }
        } else if model.type == .text_img {
            contentL.snp.remakeConstraints { (m) in
                m.leading.trailing.top.equalToSuperview().inset(20.fitR)
                m.height.equalTo(model.contentLHeight)
            }
            imgContentView.snp.remakeConstraints { (m) in
                m.leading.trailing.equalToSuperview().inset(20.fitR)
                m.top.equalTo(contentL.snp.bottom).offset(15.fitR)
                m.height.equalTo(80.fitR)
            }
          
            
        } else {
            contentL.snp.remakeConstraints { (m) in
                m.leading.trailing.top.equalToSuperview().inset(20.fitR)
                m.height.equalTo(model.contentLHeight)
            }
        }
        
        let imgCount = model.imgUrls.count
        if imgCount == 1 {
            imageV0.snp.remakeConstraints { (m) in
                m.edges.equalToSuperview()
            }
        } else if imgCount == 2 {
            let imgItemW = 0.5*(kScreenWid-3*20.fitR)
            imageV0.snp.remakeConstraints { (m) in
                m.leading.top.bottom.equalToSuperview()
                m.width.equalTo(imgItemW)
            }
            imageV1.snp.remakeConstraints { (m) in
                m.trailing.top.bottom.equalToSuperview()
                m.width.equalTo(imgItemW)
            }
        } else if imgCount >= 3 {
            let imgItemW = (kScreenWid-4*20.fitR)/3.fitR
            imageV0.snp.remakeConstraints { (m) in
                m.leading.top.bottom.equalToSuperview()
                m.width.equalTo(imgItemW)
            }
            imageV1.snp.remakeConstraints { (m) in
                m.centerX.equalToSuperview()
                m.top.bottom.equalToSuperview()
                m.width.equalTo(imgItemW)
            }
            imageV2.snp.remakeConstraints { (m) in
                m.trailing.top.bottom.equalToSuperview()
                m.width.equalTo(imgItemW)
            }
        }
    }
    
    func setupStyle() {
        backgroundColor = .color(.white)
        separateLine.backgroundColor = .color(.separateLine, 0.5)
        contentL.do { (v) in
            v.numberOfLines = 3
            v.lineBreakMode = .byTruncatingTail
            v.font = .regular(16)
            v.textColor = .color(.primary)
            v.backgroundColor = .color(.white)
            v.isHidden = true
        }
        imgContentView.do { (v) in
            v.backgroundColor = .color(.white)
            v.isHidden = true
            v.setCornerRadius(10.fitR.half)
        }
        imageV0.do { (v) in
            v.backgroundColor = .color(.viewBg)
            v.contentMode = .scaleAspectFill
            v.setCornerRadius(10.fitR.half)
            v.isHidden = true
        }
        imageV1.do { (v) in
            v.backgroundColor = .color(.viewBg)
            v.contentMode = .scaleAspectFill
            v.setCornerRadius(10.fitR.half)
            v.isHidden = true
        }
        imageV2.do { (v) in
            v.backgroundColor = .color(.viewBg)
            v.contentMode = .scaleAspectFill
            v.setCornerRadius(10.fitR.half)
            v.isHidden = true
        }
    }
}
